
import router from './router/index'

import axios from 'axios'


import {addRouterEX,addRouterEXTwo} from './routerUtils'


import RoleMenuApi from '@/api/menuRequest'





function addRouteWhile(routexs){
    for(let i in routexs){
        if(routexs[i].roleAuthorityMenuList.length > 0){
            addRouteWhile(routexs[i].roleAuthorityMenuList);
        }else{
            router.addRoute(
                'Home',
                {
                    "path": `/${routexs[i].menuRouter}`,
                    "name": routexs[i].menuRouter,
                    "meta": {
                        "title": routexs[i].menuName,
                        "id":routexs[i].menuId,
                    },
                    component: () => import(`@/views/${routexs[i].menuRouter}`)
                }
            )
        }
    }
}


const whiteList = ['/','/404','/home','/Home','/login','/myroles','/noticedetails']
//引入next必须放行
router.beforeEach((to, from, next) => {

    //  console.log("to" + to.path);

        // console.log(router.options.routes.length);

     let UserIDX = JSON.parse(sessionStorage.getItem("comploId"));

     if(UserIDX == null ){
        // console.log(from.path);
        // console.log(to.path);
        
        if(to.path == '/login' || to.path == '/'){
            next();
        }else{
            alert('请登录')
            next('/')
            throw new Error('')
        }
     }


     //如果是刷新遵循路由守卫规则
     if (whiteList.includes(to.path)) {
        // addRouterEXTwo();
        next();
     }


    if(router.getRoutes().length <= 8){
        // addRouterEX();
        //获取session路由
    let UserID = JSON.parse(sessionStorage.getItem("roleId"));
    var RouterDatax = [];
    // //如果路由为空,获取路由
    RoleMenuApi.selectRoleAuthority(UserID).then(res => {
        if(res.data == [] || res.data.length <= 0 ){
            next();
        }
        RouterDatax = res.data;
        for (let i in RouterDatax) {
            if (String(RouterDatax[i].parentId) == '0') {
                router.addRoute(
                    'Home',
                    {
                        "path": `/${RouterDatax[i].menuRouter}`,
                        "name": RouterDatax[i].menuRouter,
                        "meta": {
                            "title": RouterDatax[i].menuName
                        },
                        component: () => import(`@/views/${RouterDatax[i].menuRouter}`)
                    }
                )
                // console.log(RouterDatax[i]);
            }
        }
        addRouteWhile(RouterDatax);
        

        next({...to});

    }).catch(function (error) {

    })
        // next({...to})

    }else{
        if(from.path == '/'){

            next();
        }else{

            next();
        }
    }



   
  })
































































// //添加路由
// const menusession: any = sessionStorage.getItem("ROUTER");
// const jsonmenu = JSON.parse(menusession || '') || [];
// const modules = import.meta.glob("../views/**/**.vue");
// for (let i in jsonmenu) {

//   router.addRoute(
//     'MyHome',
//     {
//       path: jsonmenu[i].query,
//       name: jsonmenu[i].component,
//       component: modules[`../views/${jsonmenu[i].component}.vue`]
    
//     }
//   )
// }